<template>
  <div class="admin-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="220px" class="admin-sidebar">
        <div class="sidebar-header">
          <div class="logo">油柑博客</div>
          <div class="subtitle">管理后台</div>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          router
          class="sidebar-menu"
          background-color="#001529"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/admin/articles">
            <el-icon><Document /></el-icon>
            <span>文章管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/categories">
            <el-icon><Menu /></el-icon>
            <span>分类管理</span>
          </el-menu-item>
        </el-menu>
        
        <div class="sidebar-footer">
          <el-tooltip content="返回前台" placement="top">
            <el-button plain size="small" @click="goToFrontend">
              <el-icon><Back /></el-icon>
              前台首页
            </el-button>
          </el-tooltip>
        </div>
      </el-aside>
      
      <!-- 主体内容区 -->
      <el-container>
        <!-- 顶栏 -->
        <el-header height="60px" class="admin-header">
          <div class="header-left">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>{{ pageTitle }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          
          <div class="header-right">
            <el-dropdown trigger="click">
              <div class="avatar-wrapper">
                <el-avatar size="small" :src="userInfo.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"></el-avatar>
                <span class="username">{{ userInfo.nickname || userInfo.username || '管理员' }}</span>
                <el-icon><CaretBottom /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handleLogout">
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 内容区 -->
        <el-main class="admin-main">
          <router-view />
        </el-main>
        
        <!-- 底部 -->
        <el-footer height="50px" class="admin-footer">
          <div>油柑博客管理系统 &copy; {{ new Date().getFullYear() }}</div>
        </el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Document, Menu, Back, CaretBottom, SwitchButton } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userInfo = ref({})

const activeMenu = computed(() => route.path)

const pageTitle = computed(() => {
  const path = route.path
  if (path.includes('articles')) return '文章管理'
  if (path.includes('categories')) return '分类管理'
  return '管理中心'
})

const goToFrontend = () => {
  router.push('/')
}

const handleLogout = () => {
  localStorage.removeItem('user')
  ElMessage.success('退出登录成功')
  router.push('/login')
}

onMounted(() => {
  userInfo.value = JSON.parse(localStorage.getItem('user') || '{}')
  // 检查用户权限
  if (!userInfo.value.token || userInfo.value.roleId !== 1) {
    ElMessage.error('您没有管理员权限')
    router.push('/login?redirect=' + route.fullPath)
  }
})
</script>

<style scoped>
.admin-layout {
  min-height: 100vh;
}

/* 侧边栏样式 */
.admin-sidebar {
  height: 100vh;
  background-color: #001529;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
  position: relative;
  z-index: 10;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  color: white;
  padding: 20px 0;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header .logo {
  font-size: 20px;
  font-weight: bold;
}

.sidebar-header .subtitle {
  font-size: 12px;
  margin-top: 5px;
  opacity: 0.8;
}

.sidebar-menu {
  flex: 1;
  border-right: none;
}

.sidebar-footer {
  padding: 15px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  text-align: center;
}

.sidebar-footer .el-button {
  width: 100%;
}

/* 顶栏样式 */
.admin-header {
  background-color: white;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.avatar-wrapper {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
}

.username {
  font-size: 14px;
  margin: 0 8px;
  color: #606266;
}

/* 内容区样式 */
.admin-main {
  background-color: #f0f2f5;
  padding: 20px;
}

/* 底部样式 */
.admin-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #909399;
  background-color: white;
  font-size: 12px;
}

/* 移动端响应式 */
@media screen and (max-width: 768px) {
  .admin-sidebar {
    width: 64px !important;
  }
  
  .sidebar-header .logo {
    font-size: 16px;
  }
  
  .sidebar-header .subtitle {
    display: none;
  }
}
</style>